package com.yanqu.road.dao.impl.statistics;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.statistics.AbilityStatistics;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AbilityStatisticsDaoImpl extends TempDataObjectDao<AbilityStatistics> {

    public static String getAbilityStatisticsMapSql = "select * from t_u_statistics_ability where serverId=? and zeroTime >= ?";

    public Map<Long, AbilityStatistics> getAbilityStatisticsMap(long serverId, long beginTime) {
        Map<Long, AbilityStatistics> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAbilityStatisticsMapSql);
            int i = 1;
            call.setLong(i++, serverId);
            call.setLong(i++, beginTime);
            rs = call.executeQuery();
            while (rs.next()) {
                AbilityStatistics abilityStatistics = parse(rs);
                map.put(abilityStatistics.getZeroTime(), abilityStatistics);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateAbilityStatisticsSql = "update t_u_statistics_ability set ability=? where serverId=? and zeroTime=?";

    @Override
    protected boolean updateData(AbilityStatistics abilityStatistics) {
        abilityStatistics.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, abilityStatistics.getAbility()));
        para.put(i++, new DbParameter(Types.BIGINT, abilityStatistics.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, abilityStatistics.getZeroTime()));
        if (getDb().executeNoneQuery(updateAbilityStatisticsSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertAbilityStatisticsSql = "insert into t_u_statistics_ability(serverId,zeroTime,ability) values (?,?,?)";

    @Override
    protected boolean addData(AbilityStatistics abilityStatistics) {
        abilityStatistics.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, abilityStatistics.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, abilityStatistics.getZeroTime()));
        para.put(i++, new DbParameter(Types.BIGINT, abilityStatistics.getAbility()));
        if (getDb().executeNoneQuery(insertAbilityStatisticsSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected AbilityStatistics parseData(ResultSet rs) throws SQLException {
        AbilityStatistics abilityStatistics = new AbilityStatistics();
        abilityStatistics.setServerId(rs.getLong("serverId"));
        abilityStatistics.setZeroTime(rs.getLong("zeroTime"));
        abilityStatistics.setAbility(rs.getLong("ability"));
        return abilityStatistics;
    }

}
